package com.ruoyi.fly.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.fly.domain.AppletMember;
import com.ruoyi.fly.domain.CeresMember;
import com.ruoyi.fly.domain.Recommender;
import com.ruoyi.fly.domain.vo.*;
import com.ruoyi.fly.service.AppletMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;


@RestController
@RequestMapping("/appletMember")
@Api(tags = "小程序用户管理")
public class AppletMemberController extends BaseController {
    @Autowired
    private AppletMemberService appletMemberService;

    /**
     * 查询小程序用户表列表
     *
     * @param appletMember
     * @return
     */
    @ApiOperation(value = "查询用户表列表", notes = "查询用户表列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody AppletMemberSearchVo appletMember) {
        PageDomain pageDomain = new PageDomain();
        BeanUtils.copyProperties(appletMember,pageDomain);
        startPage(pageDomain);
        List<CeresMemberVo> list = appletMemberService.selectAppletMemberList1(appletMember);
        AppletMemberSearchVo appletMemberSearchVo = new AppletMemberSearchVo();
        if (appletMember.getPhone()!=null) appletMemberSearchVo.setPhone(appletMember.getPhone());
        if(appletMember.getNickname()!=null) appletMemberSearchVo.setNickname(appletMember.getNickname());
        if(appletMember.getRoleType()!=null) appletMemberSearchVo.setRoleType(appletMember.getRoleType());
        if(appletMember.getStartTime()!=null && appletMember.getEndTime()!=null){
            appletMemberSearchVo.setStartTime(appletMember.getStartTime());
            appletMemberSearchVo.setEndTime(appletMember.getEndTime());
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setTotal(appletMemberService.selectAppletMemberList1(appletMemberSearchVo).size());
        return rspData;
    }


    /**
     * 查询小程序用户表详细
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据用户id查询用户表详细")
    @GetMapping(value = "/select/{id}")
    public R<CeresMember> getInfo(@PathVariable("id") String id) {
        if (id == null){
            return R.fail("参数不能为空");
        }
        if (appletMemberService.selectAppletMemberById(id) == null) {
            return R.fail("用户不存在");
        }
        return R.ok(appletMemberService.selectAppletMemberById(id));
    }

    @ApiOperation(value = "查询推荐人信息")
    @PostMapping(value = "/selectRecommenderList")
    public R selectRecommenderList(@RequestBody RecommenderVo recommenderVo) {
        return appletMemberService.selectRecommentderList(recommenderVo);
    }


//    @PostMapping
//    public AjaxResult add(@RequestBody AppletMemberVo appletMember) {
//        return toAjax(appletMemberService.insertAppletMember(appletMember));
//    }


    /**
     * 修改小程序用户表
     *
     * @param appletMember
     * @return
     */
    //@ApiOperation(value = "修改小程序用户表")
    @PostMapping("/edit")
    public R edit(@RequestBody AppletMember appletMember) {
        return appletMemberService.updateAppletMember(appletMember);
    }

    /**
     * 删除小程序用户表
     *
     * @param deleteVo
     * @return
     */
    @ApiOperation(value = "删除小程序用户表")
    @PostMapping("/delete")
    public R remove(@RequestBody DeleteVo deleteVo) {
        if (StringUtils.isEmpty(deleteVo.getIds())){
            return R.fail("参数不能为空");
        }
        return appletMemberService.deleteAppletMemberByIds(deleteVo.getIds());
    }
}
